package pers.fjl.test.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import pers.fjl.common.po.Commodity;
import pers.fjl.common.po.Sales;
import pers.fjl.common.po.User;
import pers.fjl.common.vo.AddSales;
import pers.fjl.common.vo.SalesVo;

import java.util.List;

@Repository
public interface SalesDao extends BaseMapper<Sales> {

    /**
     * 查询所有的购买清单
     *
     * @return list
     */
    @Select("SELECT s.cashier_id, s.number, s.date, u.username, c.cname, s.oid " +
            "FROM commodity c, user u, sales s " +
            "WHERE c.cid = s.cid AND s.id = u.id ")
    List<SalesVo> getAllSales();

    /**
     * 根据用户名查找购买清单
     *
     * @param page
     * @param queryString
     * @return list
     */
    @Select("SELECT s.cashier_id, s.number, s.date, u.username, c.cname, s.oid " +
            "FROM commodity c, user u, sales s " +
            "WHERE c.cid = s.cid AND s.id = u.id AND u.username =#{queryString}")
    List<SalesVo> searchByUsername(Page<SalesVo> page, String queryString);

    /**
     * 查询根据用户名得到的购买清单的数据条数
     *
     * @param queryString
     * @return long
     */
    @Select("SELECT count(*) " +
            "FROM commodity c, user u, sales s " +
            "WHERE c.cid = s.cid AND s.id = u.id AND u.username =#{queryString}")
    long selectSearchCount(String queryString);

    /**
     * 增加购买清单
     *
     * @param addsales
     */
    @Select("SELECT u.id, c.cid" +
            "FROM user u, commodity c, sales s " +
            "WHERE u.username = #{username} AND c.cname = #{cname} AND u.username =#{queryString}")
    boolean save(AddSales addsales, String username, String cname);

    /**
     * 获取用户id
     */
    @Select("SELECT *" +
            "FROM user u " +
            "WHERE u.username = #{username} ")
    User getId(String username);

    /**
     * 获取用户id
     */
    @Select("SELECT *" +
            "FROM Commodity c " +
            "WHERE c.cname = #{cname} ")
    Commodity getCid(String cname);
}
